package com.zahb.qadx.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.model.WorkExRequest;
import com.zahb.qadx.util.InputMethodUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AddWorkExActivity extends BaseActivity {
    @BindView(R.id.etCompany)
    EditText etCompany;
    @BindView(R.id.etPost)
    EditText etPost;
    @BindView(R.id.tvStartDate)
    TextView tvStartDate;
    @BindView(R.id.tvEndDate)
    TextView tvEndDate;
    @BindView(R.id.tvSave)
    TextView tvSave;

    private SimpleDateFormat mDateTimeFormat = new SimpleDateFormat("yyyy-MM", Locale.CHINA);

    String startDate = "";
    String endDate = "";

    int data=-1;
    WorkExRequest workExRequest;

    Date selectStarDate;

    int type = 0;

    TimePickerView mTermStartPicker;

    @Override
    protected int getLayout() {
        return R.layout.activity_add_work_ex;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);

        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        String currentTime = mDateTimeFormat.format(date);

        data = getIntent().getIntExtra("datatwo",-1);
        if(data>=-1){
            workExRequest = getIntent().getParcelableExtra("data");

            if(workExRequest!=null){
                etCompany.setText(workExRequest.company);
                etPost.setText(workExRequest.job);
                startDate = workExRequest.startDate;
                tvStartDate.setText(startDate);
                if(data!=-1){
                    endDate = workExRequest.endDate;
                    tvEndDate.setText(endDate);
                }else{
                    workExRequest.endDate = currentTime;
                    endDate = workExRequest.endDate;
                    tvEndDate.setText("至今");
                }
                if(workExRequest.endDateType==0){
                    tvEndDate.setText("至今");
                }
            }
        }else{

        }





        Calendar startD = Calendar.getInstance();
        startD.set(1949,10,1);
        Calendar endD = Calendar.getInstance();

        tvStartDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodUtil.hideInputMethod(getActivity());
                mTermStartPicker = new TimePickerBuilder(getContext(), (date, v2) -> {
                    startDate = mDateTimeFormat.format(date);
                    tvStartDate.setText(startDate);
                    selectStarDate = date;
                }).setType(new boolean[]{true, true, false, false, false, false})
                        .setTitleText("请选择起始年月")
                        .setDate(endD)
                        .setRangDate(startD,endD)
                        .setLayoutRes(R.layout.pickerview_custom_time,
                                new CustomListener() {
                                    @Override
                                    public void customLayout(View v) {
                                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                                        TextView tvCancel = (TextView) v.findViewById(R.id.tv_cancel);
                                        TextView tvNow = (TextView) v.findViewById(R.id.tvNow);
                                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                mTermStartPicker.returnData();
                                                mTermStartPicker.dismiss();
                                            }
                                        });
                                        tvCancel.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                mTermStartPicker.dismiss();
                                            }
                                        });
                                        tvNow.setVisibility(View.INVISIBLE);
                                    }
                                })
                        .build();
                mTermStartPicker.show();
            }
        });

        tvEndDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodUtil.hideInputMethod(getActivity());
                Calendar  starTime = Calendar.getInstance();
                if(selectStarDate!=null){
                   starTime.setTime(selectStarDate);
                }
                 mTermStartPicker = new TimePickerBuilder(getContext(), (date, v2) -> {
                    endDate = mDateTimeFormat.format(date);
/*                    if(endDate.equals(currentTime)){
                        tvEndDate.setText("至今");
                    }else{*/
                        tvEndDate.setText(endDate);
                   // }

                }).setType(new boolean[]{true, true, false, false, false, false})
                        .setTitleText("请选择截止年月")
                        .setDate(endD)
                        .setRangDate(selectStarDate==null? startD: starTime,endD)
                        .setLayoutRes(R.layout.pickerview_custom_time,
                                new CustomListener() {
                                    @Override
                                    public void customLayout(View v) {
                                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                                        TextView tvCancel = (TextView) v.findViewById(R.id.tv_cancel);
                                        TextView tvNow = (TextView) v.findViewById(R.id.tvNow);
                                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                mTermStartPicker.returnData();
                                                mTermStartPicker.dismiss();
                                            }
                                        });
                                        tvCancel.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                mTermStartPicker.dismiss();
                                            }
                                        });
                                        tvNow.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {

                                                endDate = currentTime;
                                                tvEndDate.setText("至今");
                                                mTermStartPicker.dismiss();
                                            }
                                        });
                                    }
                                })
                        .build();
                mTermStartPicker.show();





            }
        });

        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(etCompany.getText().toString())){
                    showBindToast("请输入机构名称");
                    return;
                }
                if(TextUtils.isEmpty(etPost.getText().toString())){
                    showBindToast("请输入职务名称");
                    return;
                }
                if(TextUtils.isEmpty(startDate)){
                    showBindToast("请选择起始年月");
                    return;
                }
                if(TextUtils.isEmpty(endDate)){
                    showBindToast("请选择截止年月");
                    return;
                }
                if("至今".equals(tvEndDate.getText().toString())){

                }else if(endDate.compareTo(startDate)<0){
                    showBindToast("截止年月需大于起始年月");
                    return;
                }

                WorkExRequest workExRequest = new WorkExRequest();
                workExRequest.job = etPost.getText().toString();
                workExRequest.company = etCompany.getText().toString();
                workExRequest.startDate = startDate;
                workExRequest.endDate = endDate;

                if("至今".equals(tvEndDate.getText().toString())){
                    workExRequest.endDateType=0;
                }else{
                    workExRequest.endDateType=1;
                }

                Intent intent = new Intent();
                intent.putExtra("data",workExRequest);
                intent.putExtra("datatwo",data);
                setResult(Activity.RESULT_OK,intent);
                finish();


            }
        });

    }

    @Override
    protected int getTitleStringRes() {
        return R.string.edit_work_experience;
    }


}
